﻿package morn.core.components
{
    import flash.events.*;

    public class Container extends Box
    {
        protected var _top:Number;
        protected var _bottom:Number;
        protected var _left:Number;
        protected var _right:Number;
        protected var _centerX:Number;
        protected var _centerY:Number;

        public function Container()
        {
            addEventListener(Event.ADDED, this.onAdded);
            addEventListener(Event.REMOVED, this.onRemoved);
            return;
        }// end function

        private function onRemoved(event:Event) : void
        {
            if (event.target == this)
            {
                parent.removeEventListener(Event.RESIZE, this.onResize);
            }
            return;
        }// end function

        protected function onAdded(event:Event) : void
        {
            if (event.target == this)
            {
                parent.addEventListener(Event.RESIZE, this.onResize);
                callLater(this.resetPosition);
            }
            return;
        }// end function

        protected function onResize(event:Event) : void
        {
            callLater(this.resetPosition);
            return;
        }// end function

        public function get top() : Number
        {
            return this._top;
        }// end function

        public function set top(param1:Number) : void
        {
            this._top = param1;
            callLater(this.resetPosition);
            return;
        }// end function

        public function get bottom() : Number
        {
            return this._bottom;
        }// end function

        public function set bottom(param1:Number) : void
        {
            this._bottom = param1;
            callLater(this.resetPosition);
            return;
        }// end function

        public function get left() : Number
        {
            return this._left;
        }// end function

        public function set left(param1:Number) : void
        {
            this._left = param1;
            callLater(this.resetPosition);
            return;
        }// end function

        public function get right() : Number
        {
            return this._right;
        }// end function

        public function set right(param1:Number) : void
        {
            this._right = param1;
            callLater(this.resetPosition);
            return;
        }// end function

        public function get centerX() : Number
        {
            return this._centerX;
        }// end function

        public function set centerX(param1:Number) : void
        {
            this._centerX = param1;
            callLater(this.resetPosition);
            return;
        }// end function

        public function get centerY() : Number
        {
            return this._centerY;
        }// end function

        public function set centerY(param1:Number) : void
        {
            this._centerY = param1;
            callLater(this.resetPosition);
            return;
        }// end function

        override public function commitMeasure() : void
        {
            exeCallLater(this.resetPosition);
            return;
        }// end function

        protected function resetPosition() : void
        {
            if (parent)
            {
                if (!isNaN(this._centerX))
                {
                    x = (parent.width - displayWidth) * 0.5 + this._centerX;
                }
                else if (!isNaN(this._left))
                {
                    x = this._left;
                    if (!isNaN(this._right))
                    {
                        width = parent.width - this._left - this._right;
                    }
                }
                else if (!isNaN(this._right))
                {
                    x = parent.width - displayWidth - this._right;
                }
                if (!isNaN(this._centerY))
                {
                    y = (parent.height - displayHeight) * 0.5 + this._centerY;
                }
                else if (!isNaN(this._top))
                {
                    y = this._top;
                    if (!isNaN(this._bottom))
                    {
                        height = parent.height - this._top - this._bottom;
                    }
                }
                else if (!isNaN(this._bottom))
                {
                    y = parent.height - displayHeight - this._bottom;
                }
            }
            return;
        }// end function

    }
}
